package com.xxx.dbone.service.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xxx.dbone.dao.AccountOneMapper;
import com.xxx.dbone.service.AccountService;
import com.xxx.dbtwo.dao.AccountTwoMapper;

@Service
public class AccountServiceImpl implements AccountService {

	@Autowired
	private AccountOneMapper accountOneMapper;

	@Autowired
	private AccountTwoMapper accountTwoMapper;
	
	/**
	 * 转账
	 */
	//@Transactional
	@Override
	public int transfer(String fromAccountId, String toAccountId, BigDecimal transferAmount) {
		// 1.扣减账户1的余额
		accountOneMapper.deduct(fromAccountId, transferAmount);
		
		// 模拟分布式事务执行过程中发生异常
		 int a = 1/0;
		
		// 2.增加账户2的余额
		accountTwoMapper.increase(toAccountId, transferAmount);
		
		return 0;
	}
	
}
